<?php
/**
 * Created by PhpStorm.
 * User: zhaojunlike
 * Date: 2016/12/2
 * Time: 8:44
 */

namespace Home\Controller;


use Common\Helper\HttpHelper;

class UserController extends AuthController
{
    //短信接口
    public function getPhoneCode()
    {
        $phone = $this->user['u_phone'];
        //TODO  频繁刷
        $url = "http://api.id98.cn/api/sms";
        $param = rand(100000, 999999);
        //验证是否正确的手机号码
        $post_data = array(
            "appkey" => "8e3067c0140e3d3cc6ad64abd4becfeb",
            "phone" => "$phone",
            "templateid" => "1000",
            "param" => "$param"
        );
        $ret = HttpHelper::http($url, $post_data, 'POST');
        $retObj = json_decode($ret, true);
        if ($retObj['errcode'] == 0) {
            //发送成功了，才会写入验证码
            session('user_pwd_code', $param);
            $this->success("发送成功");
        } else {
            $this->error("发送失败");
        }
    }

    public function team()
    {
        if (IS_POST) {
            $map['admin_uid'] = $this->user['u_id'];
            $team = M('team')->where($map)->find();
            if (!$team) {
                $team['create_time'] = time();
                $team['name'] = I('name');
                $team['user_name'] = I('user_name');
                $team['user_qq'] = I('user_qq');
                $team['user_phone'] = I('user_phone');
                $team['admin_uid'] = $this->user['u_id'];
                $ret = M('team')->add($team);
                if (!$ret) {
                    $this->error("系统繁忙");
                } else {
                    $this->success("修改成功");
                }
            } else {
                //存在
                $oldId = I('id');
                if ($oldId != $team['id']) {
                    $this->error("系统繁忙,ID不一致");
                }
                $teamData['id'] = $team['id'];
                $teamData['admin_uid'] = $this->user['u_id'];
                $teamData['name'] = I('name');
                $teamData['user_name'] = I('user_name');
                $teamData['user_qq'] = I('user_qq');
                $teamData['user_phone'] = I('user_phone');
                $ret = M('team')->where(array('id' => $team['id']))->save($teamData);
                if (!$ret) {
                    $this->error("系统繁忙，修改失败。" . M('team')->getError());
                } else {
                    $this->success("修改成功");
                }
            }
        } else {
            $map['admin_uid'] = $this->user['u_id'];
            $team = M('team')->where($map)->find();
            $this->assign('_team', $team);
            $this->display();
        }
    }

    public function reset_pwd()
    {
        if (IS_POST) {
            $seCode = session('user_pwd_code');
            if (!$seCode) {
                $this->error('请先获取验证码');
            }
            $checkCode = I('post.check_code');
            if ($checkCode == $seCode) {
                $password = I('post.changepassword');
                $password2 = I('post.changepassword2');
                if ($password == $password2) {
                    $old_password = I('post.oldpassword');
                    $uMap['u_user'] = $this->user['u_user'];
                    $uMap['u_password'] = encryptUserPwd($this->user['u_user'], $old_password);
                    $rel = M('user')->where($uMap)->find();
                    if ($rel) {
                        $data['u_password'] = encryptUserPwd($this->user['u_user'], $password);
                        $last = M('user')->where($uMap)->save($data);
                        if ($last) {
                            session('user_pwd_code', null);
                            $this->success('修改密码成功');
                        } else {
                            $this->error('更改失败');
                        }
                    } else {
                        $this->error('原密码错误');
                    }
                } else {
                    $this->error('两次输入密码不一致');
                }
            } else {
                $this->error('验证码出错');
            }
        } else {
            $this->display();
        }
    }

    public function edit_profile()
    {
        $this->display();
    }

    //认证
    public function editAuth()
    {
        $this->display();
    }

    public function center()
    {
        $this->display();
    }

    public function top_profile()
    {
        if (IS_POST) {
            //查询代表队

            //存在就是修改
            $team = M('team')->where(array('u_id' => $this->user['u_id']))->find();

            //取id
            $personal = $u_user->query("select * from user where u_user='$user'");
            $u_id = $personal[0]['u_id'];
            //数据
            $data['u_id'] = $u_id;
            $data['team_name'] = I('post.teamName', '', 'htmlspecialchars');
            $data['p_name'] = I('post.captainName', '', 'htmlspecialchars');
            $p_name = $data['p_name'];
            $captainData['p_name'] = I('post.captainName', '', 'htmlspecialchars');
            $captainData['p_phone'] = I('post.captainPhone', '', 'htmlspecialchars');
            $captainData['p_qq'] = I('post.captainQq', '', 'htmlspecialchars');
            //展示
            $team_result = $team->query("select * from team where u_id='$u_id'");
            $captain_result = $team->query("select * from captain where p_name='$p_name'");
            //判断是更新还是添加
            if (empty($team_result)) {
                $team->where("u_id='$u_id'")->add($data);
                $this->success('成功', 'person', 3);
            } else {
                $team->where("u_id='$u_id'")->save($data);
                $this->success('成功', 'person', 3);
            }
            if (empty($captain_result)) {
                $captain->add($captainData);
            } else {
                $captain->where("p_name='$p_name'")->save();
            }
        } else {
            $cats = M('user')->where(array('uid' => $this->user['u_id']))->find();
            $team = M('team')->where(array('u_id' => $this->user['u_id']))->find();
            $this->assign('_caption', $cats);
            $this->assign('_team', $team);
            $this->display();
        }
    }


    public function my_grade()
    {
        if (IS_AJAX) {
            $bhID = I('bh_id');
            $join_list = M('jon_game')
                ->join("user on user.u_id=jon_game.u_id")
                ->join("jon_user on jon_game.j_id=jon_user.j_id")
                ->join("game on game.g_id=jon_game.g_id")
                ->join("backnumber on backnumber.u_id=jon_game.u_id")->where("backnumber.back_num='{$bhID}'")->select();
            $this->ajaxReturn($join_list);
        } else {
            $this->display();
        }
    }

    public function search_auth()
    {
        $realname = I('real_name');
        $idcard = I('id_card');
        $url = "http://api.id98.cn/api/idcard";
        $post_data = array(
            "appkey" => "8e3067c0140e3d3cc6ad64abd4becfeb",
            "name" => $realname,
            "cardno" => $idcard
        );
        $retDat = HttpHelper::http($url, $post_data, 'POST');
        $data = json_decode($retDat, true);
        if ($data['isok'] == 1) {
            //更新数据
            $json['data'] = $data['data'];
            $json['status'] = 0;
            switch (intval($data['code'])) {
                case 1:
                    $json['status'] = 1;
                    break;
                case 2:
                    $json['info'] = '姓名和身份证号码不一致';
                    break;
                case 3:
                    $json['info'] = '无此身份证号码';
                    break;
                default:
                    $json['info'] = '系统繁忙，请稍后重试';
                    break;
            }
            $this->ajaxReturn($json);
        } else {
            $json['status'] = 0;
            switch (intval($data['code'])) {
                case 11:
                    $json['info'] = '参数不正确';
                    break;
                case 12:
                    $json['info'] = '商户余额不足';
                    break;
                case 13:
                    $json['info'] = 'appkey不存在';
                    break;
                case 14:
                    $json['info'] = 'IP被拒绝';
                    break;
                default:
                    $json['info'] = '系统繁忙，请稍后重试';
                    break;
            }
            $this->ajaxReturn($json);
        }
    }

    public function do_auth()
    {
        if (IS_POST) {
            //个人
            if ($this->user['u_type'] == 1) {
                $user['u_pid'] = I('idcard');
                $user['u_name'] = I('realname');
                $user['u_sex'] = I('u_sex');
                $user['u_age'] = I('u_age');
                //上传图片
                $upfile = $this->uploadFile();
            }
            if ($this->user['u_type'] == 0) {
                $user['u_pid'] = I('idcard');
                $user['u_name'] = I('realname');
                //上传图片
                if ($_FILES['user_photo']['size'] != 0) {
                    $upfile = $this->uploadFile();
                    if ($upfile['user_photo']) {
                        $user['u_id_url'] = $upfile['user_photo']['savepath'] . $upfile['user_photo']['savename'];
                    }
                }
                $ret = M('user')->where(array('u_id' => $this->user['u_id']))->save($user);
                $this->success('提交认证成功，请耐心等待审核');
            }

        } else {
            $this->display();
        }
    }

    private function uploadFile()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/image/'; // 设置附件上传根目录
        $upload->autoSub = false;
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
            return $info;
        }
    }

    public function choose_auth()
    {
        if (IS_POST) {
            $map['u_id'] = $this->user['u_id'];
            $map['u_type'] = 2;
            $user = M('user')->where($map)->find();
            if (!$user) {
                $this->error('对不起,您已经选择了您得用户类型');
            }
            $ret = M('user')->where($map)->setField(array('u_type' => intval(I('auth_type'))));
            redirect(U('user/do_auth'));
        }
    }

    public function my_project()
    {
        //检测实名认证

        $this->display();
    }
}